from datetime import datetime

from flask import Flask, send_from_directory
import os
import threading
import webbrowser

app = Flask(
    __name__,
    static_folder='./static',      # 静态文件目录
    static_url_path=''             # 静态文件URL前缀
)

# API接口
@app.route('/api/data')
def get_data():
    return {
        'message': '这是来自后端的数据',
        'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    }

# 首页路由
@app.route('/')
def index():
    return send_from_directory('static', 'index.html')

# 处理Vue路由的history模式
@app.route('/<path:path>')
def catch_all(path):
    if os.path.exists(os.path.join('static', path)):
        return send_from_directory('static', path)
    else:
        return send_from_directory('static', 'index.html')

if __name__ == '__main__':
    # 自动打开浏览器
    threading.Thread(target=lambda: webbrowser.open('http://localhost:5000')).start()
    app.run(host='0.0.0.0', port=5000)